package es.udc.emarketapp.emarket.model.usersFacade;

import es.udc.emarketapp.emarket.model.client.vo.ClientVO;
import es.udc.emarketapp.emarket.model.seller.vo.SellerVO;
import es.udc.emarketapp.emarket.model.usersFacade.actions.ChangePasswordAction;
import es.udc.emarketapp.emarket.model.usersFacade.actions.CreateClientAction;
import es.udc.emarketapp.emarket.model.usersFacade.actions.CreateSellerAction;
import es.udc.emarketapp.emarket.model.usersFacade.actions.FindClientByIdAction;
import es.udc.emarketapp.emarket.model.usersFacade.actions.FindSellerByIdAction;
import es.udc.emarketapp.emarket.model.usersFacade.actions.LoginAction;
import es.udc.emarketapp.emarket.model.usersFacade.actions.ModifyClientAction;
import es.udc.emarketapp.emarket.model.usersFacade.exceptions.IncorrectPasswordException;
import es.udc.emarketapp.emarket.model.usersFacade.vo.LoginResultVO;
import es.udc.emarketapp.emarket.util.exceptions.model.NifCifAlreadyExistsException;
import es.udc.emarketapp.emarket.util.exceptions.model.UserNameAlreadyExistsException;
import es.udc.fbellas.j2ee.util.exceptions.InstanceNotFoundException;

import es.udc.fbellas.j2ee.util.exceptions.InternalErrorException;
import es.udc.fbellas.j2ee.util.exceptions.ModelException;
import es.udc.fbellas.j2ee.util.sql.PlainActionProcessor;
import es.udc.fbellas.j2ee.util.sql.SimpleDataSource;
import javax.sql.DataSource;

public class UsersFacade implements UsersFacadeInterface{

    private DataSource dataSource;

    public UsersFacade() throws InternalErrorException {

        dataSource = new SimpleDataSource();

    }

    public LoginResultVO login(String loginName, String password,
            boolean passwordIsEncrypted, boolean isAdmin)
            throws InstanceNotFoundException, IncorrectPasswordException,
            InternalErrorException {

        try {

            LoginAction action = new LoginAction(loginName, password,
                    passwordIsEncrypted, isAdmin);

            return (LoginResultVO) PlainActionProcessor.process(dataSource,
                    action);

        } catch (IncorrectPasswordException e) {
            throw e;
        } catch (InstanceNotFoundException e) {
            throw e;
        } catch (InternalErrorException e) {
            throw e;
        } catch (Exception e) {
            throw new InternalErrorException(e);
        }
    }// login

    public void changePassword(String idUser, String oldPass, String newPass)
            throws IncorrectPasswordException, InternalErrorException {

        try {

            ChangePasswordAction action = new ChangePasswordAction(idUser, oldPass, newPass);
            Object process = PlainActionProcessor.process(dataSource, action);

         } catch (IncorrectPasswordException e) {
            throw e;
        } catch (InternalErrorException e) {
            throw e;
        } catch (ModelException e) {
            throw new InternalErrorException(e);
        }



    }// changePassword




    public ClientVO createClient(ClientVO clientVO)
            throws NifCifAlreadyExistsException,
            UserNameAlreadyExistsException, InternalErrorException {

        try {

            CreateClientAction action = new CreateClientAction(clientVO);

            return (ClientVO) PlainActionProcessor.process(dataSource, action);

        } catch (InternalErrorException e) {
            throw e;
        } catch (UserNameAlreadyExistsException e) {
            throw e;
        } catch (NifCifAlreadyExistsException e) {
            throw e;
        } catch (ModelException e) {
            throw new InternalErrorException(e);
        }
    } // createClient

        public ClientVO findClientById(String idClient)
            throws InstanceNotFoundException, InternalErrorException {

        try {

            FindClientByIdAction action = new FindClientByIdAction(idClient);
            return (ClientVO) PlainActionProcessor.process(dataSource, action);

        } catch (InternalErrorException e) {
            throw e;
        } catch (InstanceNotFoundException e) {
            throw e;
        } catch (ModelException e) {
            throw new InternalErrorException(e);
        }
    }// findClientById

    public ClientVO modifyClient(ClientVO clientVO)
            throws InternalErrorException, InstanceNotFoundException {

        try {
             ModifyClientAction action = new ModifyClientAction(clientVO);
            return (ClientVO) PlainActionProcessor.process(dataSource, action);

        } catch (InstanceNotFoundException e) {
            throw e;
        } catch (InternalErrorException e) {
            throw e;
        } catch (Exception e) {
            throw new InternalErrorException(e);
        }
    }// modifyClient

    public SellerVO createSeller(SellerVO sellerVO)
            throws NifCifAlreadyExistsException,
            UserNameAlreadyExistsException, InternalErrorException {

        try {

            CreateSellerAction action = new CreateSellerAction(sellerVO);

            return (SellerVO) PlainActionProcessor.process(dataSource, action);

        } catch (InternalErrorException e) {
            throw e;
        } catch (UserNameAlreadyExistsException e) {
            throw e;
        } catch (NifCifAlreadyExistsException e) {
            throw e;
        } catch (ModelException e) {
            throw new InternalErrorException(e);
        }
    } // createSeller

    public SellerVO findSellerById(String idSeller)
            throws InternalErrorException, InstanceNotFoundException {

        try {

            FindSellerByIdAction action = new FindSellerByIdAction(idSeller);
            return (SellerVO) PlainActionProcessor.process(dataSource, action);

        } catch (InternalErrorException e) {
            throw e;
        } catch (InstanceNotFoundException e) {
            throw e;
        } catch (Exception e) {
            throw new InternalErrorException(e);
        }
    }// findSellerById

/*
        public SellerVO modifySeller(SellerVO sellerVO)
            throws InternalErrorException, InstanceNotFoundException {

        try {
             ModifySellerAction action = new ModifySellerAction(sellerVO);
            return (SellerVO) PlainActionProcessor.process(dataSource, action);

        } catch (InstanceNotFoundException e) {
            throw e;
        } catch (InternalErrorException e) {
            throw e;
        } catch (Exception e) {
            throw new InternalErrorException(e);
        }
    }// modifySeller
*/


} // class

